<?php
global $VALUTA, $MONTHS2;
$this->load->helper('form');
?>
<script language="javascript" src='<?=config_item('js_path')?>js/table_list.js?<?=time()?>'></script>
<script language="javascript" src='<?=config_item('js_path')?>js/platezh_index.js?<?=time()?>'></script>


<h1><?=lang('Список платежей')?></h1>


<!-- ----------- Фильтр. Начало ----------- -->

<a href='#' onclick="openFilter(); return false;"><img src='<?=config_item('image_path')?>images/icons/funnel.png' width='16' height='16' title='' align=left style="margin-right:5px;"> <?=lang('Фильтр')?></a><br><br>
<form method=get action='' enctype='multipart/form-data' id='filter' style='display:none;'>
<table class='edit' cellspacing=0>
<?php if($me['level'] > '*') { ?>
<tr>
	<td><?=lang('Руководитель')?>:</td>
	<td>
		<select name='user_id'>
			<?php  
			printOptionsUsers($structure, 0, @$filter['user_id']);
			?>
		</select> <input type='checkbox' name='include_children' value='1' <?php if(@$filter['include_children']) echo "checked"; ?>> <?=lang('Включать нижестоящих руководителей')?>
	</td>
</tr>
<?php } ?>
<tr>
	<td><?=lang('Валюта')?>:</td>
	<td>
		<select name='valuta'>
			<option value=''></option>
			<?php foreach($VALUTA as $k => $v) { ?>
				<option value='<?=$k?>' <?php if($k == @$filter['valuta']) echo "selected";?>><?=$v?></option>
			<?php } ?>
		</select>
	</td>
</tr>
<tr>
	<td><?=lang('Дата')?>:</td>
	<td>
			<select name='day_from'>
				<option value=''></option>
				<?php for($i = 1; $i<=31; $i++) {?>
					<option value='<?=$i?>' <?php if($i == @$filter['day_from']) echo "selected"; ?>><?=$i?></option>
				<?php } ?>
			</select> . <select name='month_from'>
				<option value=''></option>
				<?php foreach($MONTHS2 as $k => $v) {?>
					<option value='<?=$k?>' <?php if($k == @$filter['month_from']) echo "selected"; ?>><?=$v?></option>
				<?php } ?>
			</select> . <select name='year_from'>
				<option value=''></option>
				<?php for($i = 2011; $i<=(date("Y") + 1); $i++) {?>
					<option value='<?=$i?>' <?php if($i == @$filter['year_from']) echo "selected"; ?>><?=$i?></option>
				<?php } ?>
			</select>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<select name='day_to'>
				<option value=''></option>
				<?php for($i = 1; $i<=31; $i++) {?>
					<option value='<?=$i?>' <?php if($i == @$filter['day_to']) echo "selected"; ?>><?=$i?></option>
				<?php } ?>
			</select> . <select name='month_to'>
				<option value=''></option>
				<?php foreach($MONTHS2 as $k => $v) {?>
					<option value='<?=$k?>' <?php if($k == @$filter['month_to']) echo "selected"; ?>><?=$v?></option>
				<?php } ?>
			</select> . <select name='year_to'>
				<option value=''></option>
				<?php for($i = 2011; $i<=(date("Y") + 1); $i++) {?>
					<option value='<?=$i?>' <?php if($i == @$filter['year_to']) echo "selected"; ?>><?=$i?></option>
				<?php } ?>
			</select>
	</td>
</tr>
</table>
<br>
<input type='submit' value='<?=lang('Применить')?>'>
</form>

<!-- ----------- Фильтр. Конец ----------- -->



<table class='list' cellspacing=0 style="width:auto;">
<thead>
	<tr>
		<th><?=lang('Счёт')?></th>
		<th><?=lang('Дата перевода')?></th>
		<th><?=lang('Дата поступления')?></th>
		<th><?=lang('От кого')?></th>
		<th><?=lang('Кому')?></th>
		<th><?=lang('Валюта')?></th>
		<th><?=lang('Сумма')?></th>
		<th><?=lang('Комиссия')?></th>
		<th><?=lang('Итого')?></th>
		<th><?=lang('Файлы')?></th>
		<th><?=lang('Комментарий')?></th>
		<th></th>
	</tr>
</thead>
<tbody>
<?php
	foreach($platezhi as $p) 
	{
		$class = isPlatezhBuy($p) ? 'buy' : 'sell';
		?>
		<tr>
			<td><?=@$scheta[$p['schet_id']]['name']?> <?=@$VALUTA[$scheta[$p['schet_id']]['valuta']]?></td>
			<td align=center><?=date("d.m.Y", strtotime($p['from_date']))?></td>
			<td align=center><?=date("d.m.Y", strtotime($p['to_date']))?></td>
			<td><?=@$p['user_from']?></td>
			<td><?=@$p['user_to']?></td>
			<td align=center class='<?=$class?>'><?=$VALUTA[$p['valuta']]?></td>
			<td align=right class='<?=$class?>'><?=number_format($p['summ'], 2, $setting['znak_drob'], ' ')?></td>
			<td align=right class='sell'><?=number_format($p['komiss'], 2, $setting['znak_drob'], ' ')?></td>
			<td align=right class='<?=$class?>'><?=number_format($p['itogo'], 2, $setting['znak_drob'], ' ')?></td>
			<td><?php
			for($i = 1; $i <= 5; $i++)
			{
				$filename = $p['file'.$i."_comment"] ? $p['file'.$i."_comment"] : lang("Файл")." ".$i;
				if($p['file'.$i]) {?>
				<a href='<?=UPLOAD_PATH.$p['file'.$i]?>' target=_blank><?=$filename?></a><br>
				<? }
			}
			?></td>
			<td><?=nl2br(htmlspecialchars($p['comment']))?></td>
			<td class='action'>
				<a href="platezh/info/<?=$p['id']?>" title="<?=lang('Связанные операции')?>" onclick="platezhInfo(<?=$p['id']?>); return false;"><img src="<?=config_item('image_path')?>images/icons/application_list.png"></a>&nbsp;
				<?php if(isMyPlatezh($p)) { ?>
					<a href='platezh/edit/<?=$p['id']?>' title='<?=lang('Редактировать')?>'><img src='<?=config_item('image_path')?>images/icons/edit.gif'></a>
					<?php if($p['can_delete']) { ?>
						&nbsp;
						<a href='platezh/delete/<?=$p['id']?>' title='Удалить' onclick="return confirm('<?=lang('Удалить')?>?')"><img src='<?=config_item('image_path')?>images/icons/delete.png'></a>
					<?php } ?>
				<?php } ?>
			</td>
		</tr>
		<?php
}

 ?>
</tbody>
</table>

<br><br>

<?php
if(count($platezhi))
{
	?>
	<h2><?=lang('Итого')?></h2>

<table class='list' cellspacing=0 style="width:auto;">
<thead>
	<tr>
		<th></th>
		<th><?=lang('Общая сумма')?></th>
		<th><?=lang('Общая комиссия')?></th>
		<th><?=lang('Итого')?></th>
	</tr>
</thead>
<tbody>
	<tr>
		<td><?=lang('Поступило')?></td>
		<td class='buy' style="text-align:right">
			<?php
			foreach($platezhi_info['plus'] as $val => $s)
			{
				echo number_format($s, 2, $setting['znak_drob'], ' ')." ".$val."<br>";
			}
			?>
		</td>
		<td class='sell' style="text-align:right">
			<?php
			foreach($platezhi_info['komiss_plus'] as $val => $s)
			{
				echo number_format($s, 2, $setting['znak_drob'], ' ')." ".$val."<br>";
			}
			?>
		</td>
		<td class='buy' style="text-align:right">
		<?php
			foreach($platezhi_info['plus'] as $val => $s)
			{
				echo number_format(($s - $platezhi_info['komiss_plus'][$val]), 2, $setting['znak_drob'], ' ')." ".$val."<br>";
			}
			?>
		</td>
	</tr>
	<tr>
		<td><?=lang('Выплачено')?></td>
		<td class='sell' style="text-align:right">
			<?php
			foreach($platezhi_info['minus'] as $val => $s)
			{
				echo number_format($s, 2, $setting['znak_drob'], ' ')." ".$val."<br>";
			}
			?>
		</td>
		<td class='sell' style="text-align:right">
			<?php
			foreach($platezhi_info['komiss_minus'] as $val => $s)
			{
				echo number_format($s, 2, $setting['znak_drob'], ' ')." ".$val."<br>";
			}
			?>
		</td>
		<td class='sell' style="text-align:right">
		<?php
			foreach($platezhi_info['minus'] as $val => $s)
			{
				echo number_format(($s + $platezhi_info['komiss_minus'][$val]), 2, $setting['znak_drob'], ' ')." ".$val."<br>";
			}
			?>
		</td>
	</tr>
</tbody>
</table>

	<?php
}
?>
